

package edu.upn.sigemfi.plt.bc;
import edu.upn.sigemfi.plt.be.Vinculado;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class VinculadoDelegate {
  private VinculadoFacadeLocal local;

    public VinculadoDelegate() {
        local = lookupVinculadoFacade();
    }

    public void crear(Vinculado Vinculado) throws Exception{
        try {
            local.create(Vinculado);
        } catch (Exception e) {
           local = lookupVinculadoFacade();
            throw new Exception("Crear Vinculado. " + e.getMessage(),e);
        }
    }

    public void editar(Vinculado Vinculado) throws Exception{
        try {
            local.edit(Vinculado);
        } catch (Exception e) {
            local = lookupVinculadoFacade();
            throw new Exception("Editar Vinculado. " + e.getMessage(),e);
        }
    }

    public void eliminar(Vinculado Vinculado) throws Exception{
        try {
            local.remove(Vinculado);
        } catch (Exception e) {
            local = lookupVinculadoFacade();
            throw new Exception("Eliminar Vinculado. " + e.getMessage(),e);
        }
    }

    public Vinculado buscar(Long codigo) throws Exception {
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local = lookupVinculadoFacade();
            throw new Exception("Buscar Vinculado. " + e.getMessage(), e);
        }
    }

    public List<Vinculado> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupVinculadoFacade();
            throw new Exception("Listar Vinculado. " + e.getMessage(), e);
        }
    }

    private VinculadoFacadeLocal lookupVinculadoFacade() {
        try {
            Context c = new InitialContext();
            return (VinculadoFacadeLocal) c.lookup("java:comp/env/VinculadoFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }




}
